package my.xh.validate.handler.impl;

import my.xh.validate.ValidateField;
import my.xh.validate.handler.ValidateHandler;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @ClassName MaxValValidateHandler
 * @Date 2019/1/17 15:15
 * @Author xuanhu
 * @Description 最大值的校验
 */
@Component
public class MaxValValidateHandler implements ValidateHandler {
    @Override
    public boolean validate(ValidateField validateField, StringBuilder paramName, Object paramValue, Map<String, Object> map, boolean flag) {
        if (validateField.maxVal() != -1) { // 判断数值最大值
            try {
                if (Double.valueOf(paramValue.toString()) > validateField.maxVal()) {
                    flag = false;
                    map.put(paramName.toString(),  getMsg(validateField,paramName.append(" 的值不能大于").append(validateField.maxVal()).toString()));
                }
            } catch (Exception e) {
                flag = false;
                map.put(paramName.toString(),  getMsg(validateField,paramName.append(" 必须是数值类型").toString()));
            }
        }
        return  flag;
    }
}
